﻿using System;
using System.Web.UI;
using System.Data;
using VanTaiBLL;
using VanTaiProperty;

namespace VanTaiTMS.SubPage
{
    public partial class UcQuocgia : UserControl
    {
        private DataTable _dtQuocGia;
        private readonly clsTMNationalBusiness _clsQuocGia = new clsTMNationalBusiness();

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
        protected void imgSave_Click(object sender, ImageClickEventArgs e)
        {
            if (txtTenQuocGia.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào tên quốc gia !";
                OpenDialog("#ucthongbaoquocgia");
                updatePanelThongBao.Update();
            }
            if (txtMaQuocGia.Text == "")
            {
                lbThongBaoLoi.Text = "Vui lòng điền vào mã quốc gia !";
                OpenDialog("#ucthongbaoquocgia");
                updatePanelThongBao.Update();
            }
            if (txtMaQuocGia.Text != "" && txtTenQuocGia.Text != "")
            {
                InsertQuocGia();
            }
        }
        protected void imgrefesh_Click(object sender, ImageClickEventArgs e)
        {
        }
        protected void cmdCloseThongBao_Click(object sender, EventArgs e)
        {
            CloseDialog("#ucthongbaoquocgia");
        }
        protected bool CheckInsert()
        {
            bool kiemtra = false;
            _dtQuocGia = _clsQuocGia.Search("");
            if (_dtQuocGia != null && _dtQuocGia.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtQuocGia.Rows)
                {
                    if (txtMaQuocGia.Text == dr["MAQUOCGIA"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected bool CheckInsertnName()
        {
            bool kiemtra = false;
            _dtQuocGia = _clsQuocGia.Search("");
            if (_dtQuocGia != null && _dtQuocGia.Rows.Count > 0)
            {
                foreach (DataRow dr in _dtQuocGia.Rows)
                {
                    if (txtTenQuocGia.Text == dr["TENQUOCGIA"].ToString())
                    {
                        kiemtra = true;
                    }
                }
            }
            return kiemtra;
        }
        protected void InsertQuocGia()
        {
            try
            {
                if (CheckInsert())
                {
                    lbThongBaoLoi.Text = "Mã quốc gia này đã tồn tại !";
                    OpenDialog("#ucthongbaoquocgia");
                    updatePanelThongBao.Update();
                }
                else
                {
                    if (CheckInsertnName())
                    {
                        lbThongBaoLoi.Text = "Tên quốc gia này đã tồn tại !";
                        OpenDialog("#ucthongbaoquocgia");
                        updatePanelThongBao.Update();
                    }
                    else
                    {
                        var obj = new clsTMNationalProperty
                        {
                            National_Alias = txtMaQuocGia.Text,
                            National_Name = txtTenQuocGia.Text,
                            National_Foreign = chkNuocNgoai.Checked,
                            National_Note = txtGhiChu.Text
                        };
                        clsTMNationalBusiness.InsertTMNational(obj);
                        lbThongBaoLoi.Text = "Thêm mới thành công mẫu tin !";
                        OpenDialog("#ucthongbaoquocgia");
                        updatePanelThongBao.Update();
                        ClearTextbox();
                        txtMaQuocGia.Text = "QG000" + MaTuDong();
                    }
                }
            }
            catch (Exception ex)
            {
                lbThongBaoLoi.Text = ex.ToString();
                updatePanelThongBao.Update();
            }
        }
        #region[MaTangTuDong]
        protected int MaTuDong()
        {
            int i = 0;
            _dtQuocGia = _clsQuocGia.Search("");
            if (_dtQuocGia != null && _dtQuocGia.Rows.Count > 0)
            {
                int max = Convert.ToInt32(_dtQuocGia.Rows[0]["STT"].ToString());
                for (i = 0; i < _dtQuocGia.Rows.Count; i++)
                {
                    if (int.Parse(_dtQuocGia.Rows[i]["STT"].ToString()) > max)
                    {
                        max = Convert.ToInt32(_dtQuocGia.Rows[i]["STT"].ToString());
                    }
                }
                i = max + 1;
            }
            return i;
        }
        #endregion

        #region[ClearTextBox]
        protected void ClearTextbox()
        {
            txtGhiChu.Text = "";
            txtTenQuocGia.Text = "";
            txtMaQuocGia.Text = "";
            chkNuocNgoai.Checked = false;
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            var sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            var sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(Page, Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion

    }
}